package com.woniuxy.car.commodity.service.starter.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.woniuxy.common.api.enums.ResultCode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Title: SwaggerConfig
 * @ProjectName: xzhaoo0
 * @Description: TODO
 * @Author: xiong
 * @Date: 2022/2/15 19:30
 */
@Configuration
@EnableKnife4j
@EnableSwagger2WebMvc
public class SwaggerConfig {
    @Bean
    public Docket createRest(){
        List<ResponseMessage> list = new ArrayList<>();
        Arrays.stream(ResultCode.values()).forEach(e->{
            list.add(new ResponseMessageBuilder().code(e.getCode()).message(e.getMessage()).build());
        });
        return new Docket(DocumentationType.SWAGGER_2)
                .globalResponseMessage(RequestMethod.GET,list)//配置全局参数
                .globalResponseMessage(RequestMethod.POST,list)
                .globalResponseMessage(RequestMethod.DELETE,list)
                .globalResponseMessage(RequestMethod.PUT,list)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.woniuxy.car.commodity.service.core.controller"))
                .apis(RequestHandlerSelectors.basePackage("com.woniuxy.car.commodity.service.core.entity"))
                .apis(RequestHandlerSelectors.basePackage("com.woniuxy.car.commodity.service.core.dto"))
                .apis(RequestHandlerSelectors.basePackage("com.woniuxy.car.commodity.service.core.vo"))
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }
    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("商品服务接口文档")
                .version("1.0")
                .build();
    }
}
